package com.example.plat.controller;

import com.example.plat.util.ImageHandle;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Api(description = "图片上传与查看")
@RestController
@RequestMapping("/image")
public class ImageController {

    private final ResourceLoader resourceLoader;

    @Autowired
    public ImageController(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    @Value("${SavePath.path}")
    String path;

    @Autowired
    private ImageHandle handle;

    @ApiOperation("图片上传")
    @PostMapping
    public String upload(@RequestBody MultipartFile uploadFile){

        handle.upload(uploadFile);
        return uploadFile.getOriginalFilename();

    }

    @GetMapping
    private ResponseEntity show(String fileName){
        try {
            // 由于是读取本机的文件，file是一定要加上的， path是在application配置文件中的路径
            return ResponseEntity.ok(resourceLoader.getResource("file:" + path + fileName));
        } catch (Exception e) {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping("/1")
    public ResponseEntity<String> getSome(){
        return ResponseEntity.ok("NtxYJyGW9fUv5423b27d47c0bbab531e997ae8bb017d.png");
    }


}
